﻿using System.Web.UI;
using System.Text;
using System.Globalization;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using PmfWeb.Core.Utility;


namespace PmfWeb.Core.WebControls
{
    /// <summary>
    /// 自定义 SELECT
    /// </summary>
    [ToolboxData("<{0}:Select runat=server></{0}:Select>")]
    public class Select : System.Web.UI.Control, IAttributeAccessor
    {
        /// <summary>
        /// 选择的索引项
        /// </summary>
        private int selectIndex = -1;
        /// <summary>
        /// 选项列表
        /// </summary>
        private ListItemCollection items = null;
        /// <summary>
        /// 实现接口
        /// </summary>
        private AttributeCollection attrColl = null;
        /// <summary>
        /// 要显示的对象的整体文本
        /// </summary>
        private StringBuilder PrintTxt = new StringBuilder("");
        /// <summary>
        /// 显示的选项
        /// </summary>
        private StringBuilder PrintOption = new StringBuilder("");
        /// <summary>
        /// 要显示的对象ID和NAME
        /// </summary>
        public string Name = "";
        /// <summary>
        /// AttributeCollection对象
        /// </summary>
        public AttributeCollection Attributes
        {
            get
            {
                if (Utility.Utils.IsNull(this.attrColl)) this.attrColl = new PmfWeb.Core.WebControls.AttributeCollection();
                return this.attrColl;
            }
        }
        /// <summary>
        /// 实现接口定义
        /// </summary>
        /// <param name="key">属性key</param>
        /// <returns>IAttributeAccessor接口</returns>
        string IAttributeAccessor.GetAttribute(string key) { return this.Attributes[key]; }
        /// <summary>
        /// 设置接口定义
        /// </summary>
        /// <param name="key">属性key</param>
        /// <param name="value">属性value</param>
        void IAttributeAccessor.SetAttribute(string key, string value) { this.Attributes[key] = value; }
        /// <summary>
        /// 重写 System.Web.UI.Control.AddParsedSubObject 方法
        /// </summary>
        /// <param name="obj">对象</param>
        protected override void AddParsedSubObject(object obj)
        {
            if (obj is LiteralControl) this.PrintTxt = new StringBuilder(((LiteralControl)obj).Text);
            base.AddParsedSubObject(obj);
        }
        /// <summary>
        /// 设置或获取选定值
        /// </summary>
        public string Value
        {
            set
            {
                int Int = this.FindByValueInternal(value, true);
                if (Int >= 0) this.SelectedIndex = Int;
            }
            get
            {
                int Int = this.SelectedIndex;
                if ((Int >= 0) && (Int < this.Items.Count)) return this.Items[Int].Value;
                return "";
            }
        }
        /// <summary>
        /// 查找对应值的索引
        /// </summary>
        /// <param name="value">值</param>
        /// <param name="includeDisabled">条件</param>
        /// <returns>int</returns>
        private int FindByValueInternal(string value, bool includeDisabled)
        {
            for (int i = 0; i < this.items.Count; i++)
            {
                if (this.items[i].Value.Equals(value) && (includeDisabled || this.items[i].Enabled)) return i;
            }
            return -1;
        }
        /// <summary>
        /// 公共属性 Items
        /// </summary>
        public ListItemCollection Items
        {
            get
            {
                if (this.items == null) this.items = new ListItemCollection();
                return this.items;
            }
        }
        /// <summary>
        /// 判断是单选还是多选项
        /// </summary>
        public bool Multiple
        {
            get
            {
                string str = this.Attributes["multiple"];
                if (str == null) return false;
                return str.Equals("multiple");
            }
            set { this.Attributes["multiple"] = value ? "multiple" : null; }
        }
        /// <summary>
        /// 获取或设置选定的索引项
        /// </summary>
        public virtual int SelectedIndex
        {
            get
            {
                int Count = this.Items.Count;
                for (int i = 0; i < Count; i++)
                {
                    if (this.Items[i].Selected) return i;
                }
                if ((this.Size > 1) || this.Multiple) return -1;
                if (Count > 0) this.Items[0].Selected = true;
                return 0;
            }
            set
            {
                if (this.Items.Count == 0)
                    this.selectIndex = value;
                else
                {
                    if ((value < -1) || (value >= this.Items.Count)) return;
                    this.ClearSelection();
                    if (value >= 0) this.Items[value].Selected = true;
                }
            }
        }
        /// <summary>
        /// 获取或设置项目的长度
        /// </summary>
        public int Size
        {
            get
            {
                string size = this.Attributes["size"];
                if (Utility.Utils.IsNull(size)) return -1;
                return Utility.Utils.ToInt(size);
            }
            set
            {
                this.Attributes["size"] = this.MapIntegerAttributeToString(value);
            }
        }
        /// <summary>
        /// 设置是否存在对应的Size
        /// </summary>
        /// <param name="Int">是否具有值</param>
        /// <returns>string</returns>
        private string MapIntegerAttributeToString(int Int)
        {
            if (Int == -1) return null;
            return Int.ToString(NumberFormatInfo.InvariantInfo);
        }
        /// <summary>
        /// 清空所有已选择项
        /// </summary>
        private void ClearSelection()
        {
            for (int i = 0; i < this.Items.Count; i++) this.Items[i].Selected = false;
        }
        /// <summary>
        /// 重写输出模式
        /// </summary>
        /// <param name="Output">HtmlTextWriter</param>
        protected override void Render(HtmlTextWriter Output)
        {
            this.PrintTxt.Append("<select");
            if (!Utility.Utils.IsNull(this.Name)) this.PrintTxt.Append(" id=\"" + this.ClientID + "\" name=\"" + this.ClientID + "\"");
            if (!Utility.Utils.IsNull(this.Attributes)) this.PrintTxt.Append(this.Attributes);
            this.PrintTxt.Append(">");
            if (this.items != null)
            {
                int Count = this.items.Count;
                for (int i = 0; i < Count; i++)
                {
                    this.PrintTxt.Append("<option value=\"" + this.items[i].Value + "\"" + ((this.items[i].Value.Equals(this.Value)) ? " selected=\"selected\"" : "") + ">" + this.items[i].Text + "</option>");
                }
            }
            if (this.items != null)
            {
                this.items.Clear();
                this.items = null;
            }
            this.PrintTxt.Append("</select>");
            Output.Write(this.PrintTxt.ToString());
            this.PrintTxt = null;
        }
    };
}